package com.ruoyi.service.controller;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.service.entity.user.PCUserDetailRet;
import com.ruoyi.service.entity.user.PCUserListRet;
import com.ruoyi.service.service.impl.PcUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * PC用户控制
 *
 * @author ZDL
 */

@RestController
@Api(tags = "PC用户控制")
@RequestMapping("/PC/userControl")
public class PcUserController extends BaseController {

    @Autowired
    private PcUserService pcUserService;

    /**
     * 获取用户列表方法
     *
     * @param
     * @return 结果
     */

    @PreAuthorize("@ss.hasPermi('service:users:list')")
    @GetMapping("/getUserList")
    public AjaxResult getUserList(String labelStatus, Integer page, Integer size) {
        try {
            Page<PCUserListRet> retUsers = pcUserService.getUserList(labelStatus, page, size);
            return AjaxResult.success("获取成功", retUsers);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 获取用户详情方法
     *
     * @param
     * @return 结果
     */

    @PreAuthorize("@ss.hasPermi('service:users:list')")
    @GetMapping("/getUserDetail")
    public AjaxResult getUserDetail(Long userId) {
        try {
            PCUserDetailRet userDetail = pcUserService.getUserDetail(userId);
            return AjaxResult.success("获取成功", userDetail);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    //

    /**
     * 用户锁定处理方法
     *
     * @param
     * @return 结果
     */

    @PreAuthorize("@ss.hasPermi('service:users:list')")
    @PostMapping("/userLock")
    public AjaxResult userLock(@RequestBody Map<String, Object> reqBody) {
        try {
            Long userId = Long.parseLong(reqBody.get("userId").toString());
            Long reasonId = Long.parseLong(reqBody.get("reasonId").toString());
            String remark = reqBody.get("remark").toString();
            Integer result = pcUserService.userLock(userId, reasonId, remark);
            if (result == 0){
                return AjaxResult.success("用户锁定成功");
            }else {
                return AjaxResult.error("用户锁定失败:状态错误");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }


    /**
     * 用户解锁处理方法
     *
     * @param
     * @return 结果
     */

    @PreAuthorize("@ss.hasPermi('service:users:list')")
    @PostMapping("/userUnlock")
    public AjaxResult userUnlock(@RequestBody Map<String, Object> reqBody) {
        try {
            Long userId = Long.parseLong(reqBody.get("userId").toString());
            Integer result = pcUserService.userUnlock(userId);
            if (result == 0){
                return AjaxResult.success("用户解锁成功");
            }else {
                return AjaxResult.error("用户解锁失败:状态错误");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }


    /**
     * 获取用户锁定原因方法
     *
     * @param
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('service:users:list')")
    @GetMapping("/getLockReason")
    public AjaxResult getLockReason() {
        try {
            List<Map<String, Object>> stopReason = pcUserService.getLockReason();
            return AjaxResult.success("获取成功", stopReason);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

}
